0
1
00:00:00,680 --> 00:00:08,720
Now suppose we want to find the value of x that minimizes the cost - that minimizes f(x). So on the
1

2
00:00:08,720 --> 00:00:09,130
graph,
2

3
00:00:09,140 --> 00:00:13,040
we can already see that that point would be around here.
3

4
00:00:13,190 --> 00:00:15,640
But let's think about this problem a bit more deeply.
4

5
00:00:15,710 --> 00:00:15,980
Right?
5

6
00:00:16,520 --> 00:00:23,870
So if X was equal to 2, if we were here on the graph, then the cost would be decreasing a lot as we're
6

7
00:00:23,870 --> 00:00:24,500
moving down.
7

8
00:00:25,310 --> 00:00:30,230
But when X is equal to 1, then that costs still decreasing,
8

9
00:00:30,230 --> 00:00:34,830
but by not as much because the line just isn't as steep.
9

10
00:00:34,940 --> 00:00:35,880
Right?
10

11
00:00:36,020 --> 00:00:38,690
And by the same token when X is equal to 0,
11

12
00:00:38,690 --> 00:00:41,420
that line is like flattening out even more.
12

13
00:00:41,420 --> 00:00:46,280
So at this point the cost is only decreasing very, very slightly.
13

14
00:00:46,280 --> 00:00:50,560
Then we'd reach our minimum and then the cost starts increasing again,
14

15
00:00:50,630 --> 00:00:52,680
right, as we move up the curve.
15

16
00:00:52,880 --> 00:00:58,610
So in other words, the slope or the steepness of this function,
16

17
00:00:59,070 --> 00:01:07,580
yeah, is telling us when we've reached our minimum cost because that slope is the rate of change.
17

18
00:01:07,760 --> 00:01:09,750
And when this slope stops changing,
18

19
00:01:09,770 --> 00:01:14,820
well, on this graph we know that the cost is equal to the lowest value.
19

20
00:01:14,840 --> 00:01:21,200
In other words the slope of this function is kind of equal to 0 at the point where the cost is the
20

21
00:01:21,200 --> 00:01:23,180
lowest.
21

22
00:01:23,180 --> 00:01:28,150
Now you may or may not remember from school how to find the slope of a function,
22

23
00:01:28,160 --> 00:01:29,600
depends how long it's been, right?
23

24
00:01:30,230 --> 00:01:36,650
But, as a reminder, the slope of a function is given by that functions derivative.
24

25
00:01:36,650 --> 00:01:40,640
So let's add this section heading, down in the next cell.
25

26
00:01:40,640 --> 00:01:50,220
I'm going to go to "Cell" > "Cell Type" > "Markdown", put two hash tags there, and then write "Slope & Derivatives"
26

27
00:01:50,460 --> 00:01:54,820
because this is what this lesson is gonna be about. Now as a challenge,
27

28
00:01:54,950 --> 00:02:02,340
can you create a Python function for the derivative of f(x) and call that function df(x).
28

29
00:02:02,450 --> 00:02:04,470
I'm going to scroll back up for a second here.
29

30
00:02:04,550 --> 00:02:10,830
So remember f(x) was equal to x^2+x+1.
30

31
00:02:10,850 --> 00:02:12,460
I'll give you a few seconds to figure this out.
31

32
00:02:16,720 --> 00:02:18,590
All right so here's the solution.
32

33
00:02:18,590 --> 00:02:23,770
Yeah, if you were scratching your head at this point because your calculus is a little bit rusty, let
33

34
00:02:23,770 --> 00:02:28,360
me show you what the solution is and also how we arrived at it.
34

35
00:02:28,450 --> 00:02:36,040
So I'm going to define a function with the def keyword, then going to call it "df" and have one input, namely
35

36
00:02:36,050 --> 00:02:47,500
x, colon, new line, "return 2*x+1", and that's it.
36

37
00:02:47,500 --> 00:02:48,730
So how did I arrive at this?
37

38
00:02:48,730 --> 00:02:55,360
How did I know it was gonna be 2x+1? So the answer is I used calculus. Now calculus and derivatives
38

39
00:02:55,510 --> 00:03:02,800
are a pretty big topic but there's a couple of mathematical tricks or shortcuts that we can use to calculate
39

40
00:03:02,800 --> 00:03:09,730
derivatives very, very quickly and one of these tricks is called the Power Rule and we'll be applying
40

41
00:03:09,730 --> 00:03:12,290
it in several of the upcoming examples.
41

42
00:03:12,310 --> 00:03:21,940
So here's a very quick reminder on how it works, if you have a function x^n, then the
42

43
00:03:21,940 --> 00:03:29,910
derivative of this function is n * x ^ (n-1)
43

44
00:03:30,670 --> 00:03:33,160
So that's a pretty simple rule, right?
44

45
00:03:33,160 --> 00:03:38,400
Easy to remember. But before we move on let me give you a couple of quick examples.
45

46
00:03:38,490 --> 00:03:47,380
So, say we have a cost function that is x^5 and we want to calculate this cost function's
46

47
00:03:47,410 --> 00:03:48,670
derivative.
47

48
00:03:48,850 --> 00:03:55,930
If we apply the power rule then we simply get 5*x^(5-1), which is 
48

49
00:03:55,930 --> 00:03:57,770
5*x^4.
49

50
00:03:57,890 --> 00:03:59,610
And that's all there is to it.
50

51
00:03:59,680 --> 00:04:02,320
That's the function of the derivative.
51

52
00:04:02,440 --> 00:04:08,300
And here's another very, very quick example - we can apply the power rule to this function as well.
52

53
00:04:08,350 --> 00:04:12,390
x^3+5*x^2+7.
53

54
00:04:12,460 --> 00:04:16,130
So I'm going to go through these terms one by one - x^3 is pretty easy right?
54

55
00:04:16,150 --> 00:04:22,060
Because x^3 will simply be 3*x(3-1) and then for that second
55

56
00:04:22,060 --> 00:04:30,530
term, 5x^2 the five stays where it is and we have 2*x^2-1.
56

57
00:04:30,910 --> 00:04:34,050
And then that 7 right - that 7 just disappears.
57

58
00:04:34,060 --> 00:04:37,980
That 7 just drops because 7 is a constant.
58

59
00:04:37,990 --> 00:04:46,180
It doesn't depend on X - constants don't change, and since a derivative is all about the rate of change
59

60
00:04:46,510 --> 00:04:47,830
the constant drops out.
60

61
00:04:48,040 --> 00:04:48,910
It's equal to 0.
61

62
00:04:49,030 --> 00:04:51,100
So the 7 disappears.
62

63
00:04:51,100 --> 00:04:54,610
So what we end up with is 3*x^2
63

64
00:04:54,610 --> 00:05:02,800
+ 5*2*x, right, 10*x, and that's it.
64

65
00:05:02,800 --> 00:05:03,040
Right.
65

66
00:05:03,040 --> 00:05:09,220
That's the power rule for derivatives in a nutshell. So back in Jupyter notebook,
66

67
00:05:09,280 --> 00:05:13,590
say we want a graph our derivative function that we've just worked out here.
67

68
00:05:13,750 --> 00:05:15,450
We've worked hard to create it.
68

69
00:05:15,460 --> 00:05:20,560
So let's visualize it side by side with our original cost function.
69

70
00:05:21,190 --> 00:05:29,200
So what we want is we want to charts, side by side, and to do that we're going to have to use the so-called
70

71
00:05:29,470 --> 00:05:32,680
subplot functionality from matplotlib.
71

72
00:05:32,680 --> 00:05:38,910
So I'm gonna show you the subplot functionality and I'm also gonna show you how to size the two charts.
72

73
00:05:39,070 --> 00:05:40,420
So this is something new.
73

74
00:05:40,420 --> 00:05:45,400
This is something we haven't done before, but since a lot of the code will be very, very similar to what
74

75
00:05:45,400 --> 00:05:53,470
we've had up here I'm just gonna very, very quickly go to "Edit" > "Copy Cell" and then go down here
75

76
00:05:54,340 --> 00:06:03,150
and I'm going to go to "Edit" > "Paste Cell Above", so I can reuse some of this code that we've got here. And
76

77
00:06:03,190 --> 00:06:10,810
I'm going to change my comment here and I say "Plot function and derivative side by side".
77

78
00:06:10,810 --> 00:06:16,960
So this is what we're gonna do here, and uh, in order to size our plot,
78

79
00:06:17,000 --> 00:06:17,340
yeah,
79

80
00:06:17,440 --> 00:06:23,100
in order to size it we're gonna have to set the figure size of our plot.
80

81
00:06:23,260 --> 00:06:33,810
So we're gonna write "plt.figure()", and then we're gonna say "figsize="
81

82
00:06:33,820 --> 00:06:36,450
and then we have to give it a width and height.
82

83
00:06:36,550 --> 00:06:46,570
So let's see what happens if I put in, I don't know, "10,5" in the square brackets and hit Shift+
83

84
00:06:46,570 --> 00:06:54,150
Enter, so you can see now that the width is 10 and the height is 5.
84

85
00:06:54,160 --> 00:06:59,530
I looked at the documentation for this and this is actually measured in inches of all things.
85

86
00:06:59,560 --> 00:07:00,560
I don't really know.
86

87
00:07:00,820 --> 00:07:06,850
I don't really know why that is but I hope it makes our American friends happy.
87

88
00:07:06,920 --> 00:07:14,890
Now I'm going to modify this code a little bit more, so in order to have two plots side by side we're
88

89
00:07:14,890 --> 00:07:23,740
gonna have to use this subplot functionality from matplotlib, so I'm going to use "plt.subplot" and then
89

90
00:07:23,740 --> 00:07:30,710
I have to specify three numbers - a row, a column and an index.
90

91
00:07:30,730 --> 00:07:34,490
So in this case I'm going to have two plots, side by side.
91

92
00:07:34,570 --> 00:07:35,970
So they're going to be in the same row.
92

93
00:07:36,010 --> 00:07:38,820
So one row is going to be two columns.
93

94
00:07:39,010 --> 00:07:46,250
And the first plot, this cost function here is gonna be, at index 1, is gonna be my first plot. And now
94

95
00:07:46,250 --> 00:07:47,930
it's time to add our second plot.
95

96
00:07:47,980 --> 00:07:51,600
So, I'm going to say "plt.subplot"
96

97
00:07:51,830 --> 00:07:57,290
And for the second plot it's gonna be still in the same row - row number one, still two columns, but this
97

98
00:07:57,290 --> 00:07:59,330
one is gonna be at index 2.
98

99
00:07:59,330 --> 00:08:01,270
So this is gonna be my second plot.
99

100
00:08:01,300 --> 00:08:02,950
Now I haven't added it yet, I'm going to do that
100

101
00:08:02,950 --> 00:08:10,210
now. To graph our derivative, I'm going to write "plt.plot" and we're going to feed in our 
101

102
00:08:10,270 --> 00:08:13,090
x_1 values and then we're gonna call our function.
102

103
00:08:13,100 --> 00:08:13,350
Right.
103

104
00:08:13,490 --> 00:08:23,290
So the first one was f(x_1) and this one is gonna be df(x_1). Let's see what happens
104

105
00:08:23,290 --> 00:08:25,410
now. I'm going to press Shift+Enter.
105

106
00:08:25,420 --> 00:08:26,160
Huh.
106

107
00:08:26,280 --> 00:08:27,620
So I've got two plots
107

108
00:08:27,630 --> 00:08:34,530
now. You see this? Two plots side by side and they're still occupying the same space as before, right.
108

109
00:08:34,560 --> 00:08:38,320
The overall figure is still 10 by 5.
109

110
00:08:38,370 --> 00:08:46,170
So we can see here the figure size hasn't changed, but now it's split with two plots, two subplots taking
110

111
00:08:46,170 --> 00:08:47,760
up that same space.
111

112
00:08:47,760 --> 00:08:53,400
So I can probably make use of this space here on the right a little bit better by maybe making this
112

113
00:08:53,720 --> 00:08:54,910
15 by 5.
113

114
00:08:54,920 --> 00:08:56,860
I think that should be about right.
114

115
00:08:56,910 --> 00:08:57,690
Press Shift+Enter.
115

116
00:08:57,690 --> 00:08:59,180
I get it like this.
116

117
00:08:59,250 --> 00:09:00,480
Very nice.
117

118
00:09:00,790 --> 00:09:06,780
Now what I'm going to do is add a few labels and style these graphs a little bit better so that if you
118

119
00:09:06,780 --> 00:09:11,550
ever come back to this notebook in the future, you can make sense of these charts very quickly and very
119

120
00:09:11,550 --> 00:09:12,070
easily.
120

121
00:09:12,120 --> 00:09:15,160
So, I'm going to insert two comments first, right.
121

122
00:09:15,210 --> 00:09:23,880
So this is the first chart and this is the cost function and down here is the second chart
122

123
00:09:28,800 --> 00:09:29,760
of the derivative.
123

124
00:09:33,540 --> 00:09:41,760
For the first chart let's explicitly state that it should be blue and that it should have a line width
124

125
00:09:43,110 --> 00:09:55,710
of maybe 3 and for our second chart we're gonna give it the color sky blue and a line width of
125

126
00:09:55,990 --> 00:09:56,640
5.
126

127
00:09:59,790 --> 00:10:00,450
How does that look?
127

128
00:10:03,170 --> 00:10:04,300
Pretty good.
128

129
00:10:04,340 --> 00:10:10,130
Now let's add some title and X labels and Y labels to that second chart as well.
129

130
00:10:10,130 --> 00:10:19,350
So I'm just gonna copy this code here and add it here but instead of having it say "Cost function", I'm
130

131
00:10:19,350 --> 00:10:27,890
going to say "Slope of the Cost function" and on the x is still x, but on the y it's gonna be "df(x)"
131

132
00:10:29,580 --> 00:10:37,590
and then I'm also gonna set the axes, so again I'm going to copy xlim and ylim and add this down here,
132

133
00:10:38,470 --> 00:10:49,150
but I'm gonna have it go from maybe -2 to 3, and on the Y from say, 3 to 6.
133

134
00:10:49,260 --> 00:10:51,530
Oh sorry, -3 to 6.
134

135
00:10:54,310 --> 00:10:56,970
I'm going to hit Shift+Enter to see what it looks like.
135

136
00:10:57,070 --> 00:10:58,300
So this is quite good.
136

137
00:10:58,300 --> 00:11:03,070
This is very, very nice and clear but there's one small improvement that I'd really like to make.
137

138
00:11:03,100 --> 00:11:08,560
We're interested primarily in where this minimum is here and one of the things that be quite nice to
138

139
00:11:08,560 --> 00:11:19,060
see on this chart is where our slope, our sky blue line here, actually intersects the axis here.
139

140
00:11:19,060 --> 00:11:25,150
So what we can do is we can style our chart to have a grid.
140

141
00:11:25,510 --> 00:11:33,460
This is fairly easy to do so for our second chart for our derivative all we have to do is add "plt.
141

142
00:11:34,240 --> 00:11:39,220
grid" and then two parentheses for the method name.
142

143
00:11:39,220 --> 00:11:41,690
No need to add any arguments here.
143

144
00:11:41,980 --> 00:11:51,220
Hit Shift+Enter and then you can see matplotlib applies a grid automatically to this subplot and now
144

145
00:11:51,220 --> 00:11:59,560
it's very, very nice to see that our intersection of the slope of the derivative is going to be probably
145

146
00:11:59,770 --> 00:12:07,650
somewhere at the midpoint between -1 and 0, so that's gonna be around here somewhere.
146

147
00:12:07,690 --> 00:12:09,380
That seems about right.
147

148
00:12:09,460 --> 00:12:14,490
So as a quick summary we've covered a three new things for styling graphs and charts.
148

149
00:12:14,530 --> 00:12:17,890
One of them is this idea of sizing them.
149

150
00:12:17,950 --> 00:12:26,590
The second thing is this idea of having plots side by side and that requires the subplot function which
150

151
00:12:26,830 --> 00:12:31,800
needs to know how many rows, how many columns and what the index is.
151

152
00:12:32,080 --> 00:12:41,160
And then also we've used the grid function for the first time to superimpose a grid on to the plot.
152

153
00:12:41,260 --> 00:12:50,820
Now as a challenge, can you instead of having these plots side by side, stack them from top to bottom?
153

154
00:12:50,920 --> 00:12:56,830
What would you have to change in the Python code in order to achieve this? In order to arrange the plots
154

155
00:12:56,830 --> 00:12:57,800
in a different way.
155

156
00:12:57,820 --> 00:13:04,980
I'll give you a few seconds to pause the video and try it out. Did you solve it?
156

157
00:13:04,980 --> 00:13:06,180
Did you manage to get it?
157

158
00:13:06,180 --> 00:13:07,830
Here's the solution.
158

159
00:13:07,830 --> 00:13:13,740
The key on arranging the charts is gonna be where our subplots are, right?
159

160
00:13:13,740 --> 00:13:22,050
So if you want to have them top to bottom, then you'd have to have two rows and one column and I'd have
160

161
00:13:22,050 --> 00:13:24,750
to change this for both subplots.
161

162
00:13:24,750 --> 00:13:32,070
So for the first chart and for the second chart I'm going to have to go 2 and 1 instead of 1 and 2. If
162

163
00:13:32,070 --> 00:13:40,020
I press Shift+Enter now, we can see our charts arrange themselves from top to bottom. But one of the reasons
163

164
00:13:40,020 --> 00:13:48,540
why they look terrible like this is because we've constrained our figure size to be still using the
164

165
00:13:48,540 --> 00:13:51,950
same dimensions - 15 by 5, right?
165

166
00:13:52,020 --> 00:13:57,430
So it's stretching out the charts across the whole space that it's allotted.
166

167
00:13:57,690 --> 00:13:59,710
And that's why they look like this.
167

168
00:13:59,970 --> 00:14:08,570
But if we changed our figure size with some new constraints say 5 and 15, right, then pressing Shift+Enter
168

169
00:14:08,570 --> 00:14:11,180
will give us the charts like this.
169

170
00:14:11,210 --> 00:14:14,960
In this case they're more tall than wide.
170

171
00:14:15,200 --> 00:14:23,780
Now I quite like the original setup so I'm going to change this back to 15 and 5 and I'm going to go
171

172
00:14:23,780 --> 00:14:32,300
with one row, two columns for my subplots - one row and two columns for both of them.
172

173
00:14:32,330 --> 00:14:36,700
So this is how I would prefer to look at these two charts.
173

174
00:14:36,830 --> 00:14:44,420
But yeah, if you figure this out super handy for being able to arrange the charts in your Jupyter notebook.
174

175
00:14:45,340 --> 00:14:46,150
All right.
175

176
00:14:46,150 --> 00:14:48,200
So I hope you're ready for the next lesson.
176

177
00:14:48,370 --> 00:14:48,780
I'll see
177

178
00:14:48,780 --> 00:14:49,600
you there.
178

179
00:14:49,630 --> 00:14:50,120
Take care.
